package com.longnuoqi.mydoctor.utils;

import android.content.Context;
import android.graphics.Bitmap;
import android.util.Log;
import android.util.LruCache;
import android.widget.ImageView;
import android.widget.Toast;

import com.android.volley.Request;
import com.android.volley.RequestQueue;
import com.android.volley.Response;
import com.android.volley.VolleyError;
import com.android.volley.toolbox.ImageLoader;
import com.android.volley.toolbox.ImageRequest;
import com.android.volley.toolbox.StringRequest;
import com.android.volley.toolbox.Volley;
import com.yunhe.mydoctor.R;

import org.json.JSONException;
import org.json.JSONObject;

import java.io.UnsupportedEncodingException;
import java.net.URLEncoder;

public class VolleyUtil {
    public static void initFavorite(final Context context, int id, String type, String access_token, String title) {
        try {
            String strUtf8 = URLEncoder.encode(title, "utf-8");
            String url = "http://www.tngou.net/api/favorite/add" + "?id=" + id + "&type=" + type + "&access_token=" + access_token + "&title=" + strUtf8;
            RequestQueue queue = Volley.newRequestQueue(context);
            StringRequest request = new StringRequest(Request.Method.GET, url, new Response.Listener<String>() {
                @Override
                public void onResponse(String s) {
                    try {
                        JSONObject data = new JSONObject(s);
                        boolean status = data.getBoolean("status");
                        String msg = "";
                        if (status) {
                            int favour = data.getInt("favorite");
                            switch (favour) {
                                case 0:
                                    msg = "未收藏";
                                    break;
                                case 1:
                                    msg = "收藏成功";
                                    break;
                                case 2:
                                    msg = "已收藏";
                                    break;
                                default:
                                    break;
                            }
                        } else {
                            msg = data.getString("msg");
                        }
                        Toast.makeText(context, msg, Toast.LENGTH_SHORT).show();
                    } catch (JSONException e) {
                        e.printStackTrace();
                    }

                }
            }, new Response.ErrorListener() {
                @Override
                public void onErrorResponse(VolleyError volleyError) {
                }
            });
            queue.add(request);
        } catch (UnsupportedEncodingException e) {
            e.printStackTrace();
        }

    }

    public void getImageRequest(Context context, String url, final ImageView img, final String name) {
        RequestQueue queue = Volley.newRequestQueue(context);
        ImageRequest imageRequest = new ImageRequest(url, new Response.Listener<Bitmap>() {
            @Override
            public void onResponse(Bitmap bitmap) {
                Log.e("=================", "imagerequest success");
                img.setImageBitmap(bitmap);
                FileUtil.saveByte2File(FileUtil.Bitmap2Bytes(bitmap), name);
            }
        }, 0, 0, null, Bitmap.Config.RGB_565, new Response.ErrorListener() {
            @Override
            public void onErrorResponse(VolleyError volleyError) {
                Log.e("=================", "cant imagerequest");
                img.setImageResource(R.mipmap.image_none);
            }
        });
        queue.add(imageRequest);
    }

    public void getImageLoader(Context context, String url, ImageView img) {
        RequestQueue queue = Volley.newRequestQueue(context);
        ImageLoader loader = new ImageLoader(queue, new MyImageCache());
        ImageLoader.ImageListener listener = ImageLoader.getImageListener(img, R.mipmap.image_none, R.mipmap.image_none);
        loader.get(url, listener);
    }

    private class MyImageCache implements ImageLoader.ImageCache {
        private LruCache<String, Bitmap> mCache;//声明一个缓存对象

        public MyImageCache() {
            int maxSize = 5 * 1024 * 1024;
            mCache = new LruCache<String, Bitmap>(maxSize) {
                @Override
                protected int sizeOf(String key, Bitmap bitmap) {
                    return bitmap.getRowBytes() * bitmap.getHeight();
                }
            };//实例化缓存对象并声明大小
        }

        @Override
        public Bitmap getBitmap(String url) {
            return mCache.get(url);
        }

        @Override
        public void putBitmap(String url, Bitmap bitmap) {
            mCache.put(url, bitmap);
        }
    }
}
